package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class OrdersController {
    /**
     *  根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，​
     * 并获取自动生成的订单编号，​ 然后根据用户编号、商家编号从购物车表中查询所有数据，
     * 批量添加到订单明细表中，​ 然后根据用户编号、商家编号删除购物车表中的数据。
     * @param
     * @return  orderId
     * @throws Exception
     */
    public int createOrders(HttpServletRequest request) throws Exception{
        Orders orders = new Orders();
        orders.setUserId(request.getParameter("userId"));
        orders.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        orders.setOrderDate(request.getParameter("orderDate"));
        orders.setOrderTotal(Double.valueOf(request.getParameter("orderTotal")));
        orders.setDaId(Integer.valueOf(request.getParameter("daId")));
        orders.setOrderState(Integer.valueOf(request.getParameter("orderState")));
        OrdersServiceImpl ordersService = new OrdersServiceImpl();
        int orderId = ordersService.createOrders(orders);
        return orderId;
    }

    /**
     * 根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     * @param
     * @return
     * @throws Exception
     */
    public Orders getOrdersById(HttpServletRequest request)throws Exception{
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        OrdersServiceImpl ordersService = new OrdersServiceImpl();
        return ordersService.getOrdersById(orderId);
    }

    public List<Orders> listOrdersByUserId(HttpServletRequest request)throws Exception{
        List<Orders> list = new ArrayList<>();
        String userId = request.getParameter("userId");
        OrdersServiceImpl ordersService = new OrdersServiceImpl();
        list = ordersService.listOrdersByUserId(userId);
        return list;
    }
}
